﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Linq.Expressions;

namespace AnChao.WPF.Controls.TextBox.Controls
{
    /// <summary>
    /// 实现属性变化通知的对象基类
    /// </summary>
    [Serializable]
    public class NotifyObjectBase : INotifyPropertyChanged, INotifyPropertyChanging
	{
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;
        [field: NonSerialized]
        public event PropertyChangingEventHandler PropertyChanging;

		protected void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}

        protected virtual void OnPropertyChanged<T>(Expression<Func<T>> propertyExpression)
        {
            if (propertyExpression == null)
                return;

			if (PropertyChanged != null)
            {
                var body = propertyExpression.Body as MemberExpression;
				PropertyChanged(this, new PropertyChangedEventArgs(body.Member.Name));
            }
        }

        protected void OnPropertyChanging(string propertyName)
		{
			if (PropertyChanging != null)
				PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
		}

        protected virtual void OnPropertyChanging<T>(Expression<Func<T>> propertyExpression)
        {
            if (propertyExpression == null)
                return;

			if (PropertyChanging != null)
            {
                var body = propertyExpression.Body as MemberExpression;
				PropertyChanging(this, new PropertyChangingEventArgs(body.Member.Name));
            }
        }

	}
}
